#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#include "user.h"
#include "room.h"
#include "customer.h"

#define CUSTOMER_FILE "customer.txt"

int customerMenu()
{
	int c;
	showTime(1);
	printf("\t\t\t        旅店管理系統\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t          旅客查询\n");
	printf("\t\t\t   * 1 查询房间\n");
	printf("\t\t\t   * 2 查看旅客信息\n");
	printf("\t\t\t   * 0 注销\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t请选择:");
	scanf("%d",&c);
	getchar();
	while(c < 0||c > 2)
	{
		printf("请重新输入:");
		scanf("%d",&c);
		getchar();
	}
	return c ;


}


void customerSystem(customer *r)
{
	int c,loop = 1;
	Room *ro;
	while(loop)
	{
		system("clear");
		c = customerMenu();
		switch(c)
		{
			case 1:
				printf(RED);
				printf("show room\n");
				printf(NONE);
				ro = read_room();
				display_room(ro);
				printf("输入任意指令以继续:\n");
				getchar();
				break;
			case 2:
				printf(RED);
				printf("show me\n");
				printf(NONE);
				show_me();
				break;
			case 0:
			default:
				loop = 0;
				break;
		}//switch(c)
	}
}

int write_customer(customer *r)
{
	FILE *fp;

	fp = fopen(CUSTOMER_FILE,"a");
	if (fp == NULL)
	{
		perror("打开customer.txt失败");
		return 1;
	}
	fprintf(fp,"%-2d",r->roomId);
	fprintf(fp, "%-19s",r->m_ID);
	fprintf(fp, "%-9s",r->m_name);
	fprintf(fp, "%-2d",r->m_age);
	fprintf(fp, "%-4s",r->m_sex);
	fprintf(fp, "%-12s",r->phone);
	fprintf(fp, "%-12s",r->inDay);
	fprintf(fp, "%-12s",r->outDay);
	fprintf(fp, "%-2d\n",r->status);

	if (fp)
		fclose(fp);
	return 0;
}

int write_all_customer(customer *head)
{
	FILE *fp;
	customer *r;

	fp = fopen(CUSTOMER_FILE,"w");
	if (fp == NULL)
	{
		perror("打开customer.txt失败");
		return 1;
	}
	r = head;
	while(r->next)
	{
		fprintf(fp,"%-2d",r->roomId);
		fprintf(fp, "%-19s",r->m_ID);
		fprintf(fp, "%-9s",r->m_name);
		fprintf(fp, "%-2d",r->m_age);
		fprintf(fp, "%-4s",r->m_sex);
		fprintf(fp, "%-12s",r->phone);
		fprintf(fp, "%-12s",r->inDay);
		fprintf(fp, "%-12s",r->outDay);
		fprintf(fp, "%-2d\n",r->status);
		r = r->next;
	}
	if (fp) fclose(fp);
	return 0;
}


customer* read_customer()
{
	customer *head,*p,*newNode;
	customer *tp;
	FILE *fp;
	fp = fopen (CUSTOMER_FILE,"r");
	if (fp == NULL)
	{
		perror("open customer file read");
		return NULL;
	}
	head = (customer *)malloc(sizeof(customer));
	if (NULL == head)
	{
		printf("malloc fail\n");
		if(fp) fclose(fp);
		return NULL;
	}
	memset(head,0,sizeof(customer));	
	p = head;
	while(!feof(fp))
	{
		fscanf(fp,"%2d",&p->roomId);
		fscanf(fp, "%s",p->m_ID);
		fscanf(fp, "%s",p->m_name);
		fscanf(fp, "%2d",&p->m_age);
		fscanf(fp, "%s",p->m_sex);
		fscanf(fp, "%s",p->phone);
		fscanf(fp, "%s",p->inDay);
		fscanf(fp, "%s",p->outDay);
		fscanf(fp, "%2d",&p->status);

		newNode = (customer*)malloc(sizeof(customer));
		memset(newNode,0,sizeof(customer));
		p->next = newNode;
		tp = p;
		p = newNode;
	}
	tp->next = NULL;
	free(p);
	if(fp) fclose(fp);
	return head;
}

void show_me()
{
	customer *head,*p;
	int i;
	char s[9];
	head = read_customer();
	p = head;
//	printf("%-2d  %-19s  %-9s  %-2d  %s  %-12s  %-12s  %-12s  %d \n",p->roomId,p->m_ID,p->m_name,p->m_age,p->m_sex,p->phone,p->inDay,p->outDay,p->status);
	printf("请输入旅客姓名:\n");
	scanf("%s",s);
	getchar();
	while(p->next != NULL)
	{
		i = strcmp(s,p->m_name);
		if (i == 0)
			break;
		p = p->next;
	}
	if (p->next == NULL)
	{
		printf("找不到旅客姓名!\n");
		printf("输入任意指令以继续: \n");
		getchar();
		return ;
	}
//	printf("roomId  m_ID   name  age  sex  phone  inDay  outDay  status\n");
//	printf("%-2d  %-19s  %-9s  %-2d  %s  %-12s  %-12s  %-12s  %d \n",p->roomId,p->m_ID,p->m_name,p->m_age,p->m_sex,p->phone,p->inDay,p->outDay,p->status);
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex  phone        status\n");
	printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);

	printf("输入任意指令以继续: \n");
	getchar();
	return ;
}

 	
int register_rz()
{
	customer *pr;
	pr = (customer*)malloc(sizeof(customer));
		if(pr == NULL)
		{
			return 1;
		}
		memset(pr,0,sizeof(customer));
		printf("请输入房间号:\n");
		scanf("%d",&pr->roomId);
		getchar();
		printf("请输入顾客身份证号码:\n");
		fgets(pr->m_ID,19,stdin);
		getchar();
		printf("请输入顾客名字:\n");
		fgets(pr->m_name,8,stdin);
		printf("请输入顾客年龄:\n");
		scanf("%d",&pr->m_age);
		getchar();
		printf("请输入顾客性别(m/f):\n");
		fgets(pr->m_sex,4,stdin);
		printf("请输入顾客电话号码:\n");
		fgets(pr->phone,12,stdin);
		printf("请输入开房时间:\n");
		fgets(pr->inDay,12,stdin);
		printf("请输入退房时间:\n");
		fgets(pr->outDay,12,stdin);
		pr->status = 1;
	
		write_customer(pr);
		return 0;
}

int register_yy()
{
	customer *pr;
	pr = (customer*)malloc(sizeof(customer));
		if(pr == NULL)
		{
			return 1;
		}
		memset(pr,0,sizeof(customer));
		printf("请输入房间号:\n");
		scanf("%d",&pr->roomId);
		getchar();
		printf("请输入顾客身份证号码:\n");
		fgets(pr->m_ID,19,stdin);
		getchar();
		printf("请输入顾客名字:\n");
		fgets(pr->m_name,8,stdin);
		printf("请输入顾客年龄:\n");
		scanf("%d",&pr->m_age);
		getchar();
		printf("请输入顾客性别(m/f):\n");
		fgets(pr->m_sex,4,stdin);
		printf("请输入顾客电话号码:\n");
		fgets(pr->phone,12,stdin);
		printf("请输入开房时间:\n");
		fgets(pr->inDay,12,stdin);
		printf("请输入退房时间:\n");
		fgets(pr->outDay,12,stdin);
		pr->status = 2;
	
		write_customer(pr);
		return 0;
}

int free_customer()
{
	customer *head,*p;
	int s;
	head = read_customer();
	p = head;

	printf("请重新输入退房的房间号:\n");
	scanf("%d",&s);	
	getchar();

	while(p->next)
	{
		if(s == p->roomId && (p->status == 1 || p->status == 2))
		{
		//	printf("find\n");
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号对应的旅客\n");	
		return 1;			
	}
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex  phone        status\n");
	printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);
	p->status = 0;
	printf("旅客退房成功！\n");
	write_all_customer(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;
}

int exchange_room_customer()
{
	customer *head,*p;
	char s[9];
	int i;
	head = read_customer();
	p = head;

	printf("请输入旅客姓名:\n");
	scanf("%s",s);
	getchar();
	while(p->next)
	{
		if (!strcmp(s,p->m_name))
			break;
		p = p->next;
	}
	if (!p->next)
	{
		printf("找不到旅客姓名!\n");
		return 1;
	}
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex  phone        status\n");
	printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);
	printf("请重新输入要调换的房间号:\n");
	scanf("%d",&i);
	getchar();
	p->roomId = i;
	printf("旅客换房成功！\n");
	write_all_customer(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;
}

int keep_room_customer()
{
	customer *head,*p;
	char s[9];
	head = read_customer();
	p = head;

	printf("请输入旅客姓名:\n");
	scanf("%s",s);
	getchar();
	while(p->next)
	{
		if (!strcmp(s,p->m_name))
			break;
		p = p->next;
	}
	if (!p->next)
	{
		printf("找不到旅客姓名!\n");
		return 1;
	}
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex inDay         outDay       status\n");
	printf("%-2d       %-9s %-2d   %s   %-12s  %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->inDay,p->outDay,p->status);
	printf("请输入要续住到的日期:\n");
	scanf("%s",p->outDay);
	getchar();
	
	write_all_customer(head);

//	printf("输入任意指令以继续:\n");
//	getchar();
	return 0;
}

int find_customer_roomId()
{
	customer *head,*p;
	int s;
	head = read_customer();
	p = head;

	printf("请输入房间号:\n");
	scanf("%d",&s);	
	getchar();

	while(p->next)
	{
		if(s == p->roomId && (p->status == 1 || p->status == 2))
		{
		//	printf("find\n");
			break;
		}
		p = p->next;
	}
	if(!p->next)
	{
		printf("没有找到此房间号对应的旅客\n");	
		return 1;			
	}
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex  phone        status\n");
	printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);

	return 0;
}

int find_customer_name()
{
	customer *head,*p;
	char s[9];
	head = read_customer();
	p = head;

	printf("请输入旅客姓名:\n");
	scanf("%s",s);
	getchar();
	while(p->next)
	{
		if (!strcmp(s,p->m_name))
			break;
		p = p->next;
	}
	if (!p->next)
	{
		printf("找不到该姓名对应的旅客!\n");
		return 1;
	}
	printf("搜索到该旅客信息:\n");
	printf("roomId   name      age  sex  phone        status\n");
	printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);

	return 0;	
}

int find_customer_all()
{
	customer *head,*p;
	head = read_customer();
	p = head;

	printf("roomId   name      age  sex  phone        status\n");
	while(p->next)
	{
		printf("%-2d       %-9s %-2d   %s    %-12s %d \n",p->roomId,p->m_name,p->m_age,p->m_sex,p->phone,p->status);
		p = p->next;
	}
	return 0;
}